<?php

$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
set_include_path(get_include_path().PATH_SEPARATOR.$baseDir.PATH_SEPARATOR.$baseDir.'/incubator');

include '../../testingfunctions.php';
include 'Pone/Pone_Exception.php';
include 'Pone/Database/Pone_Database_ConnectionFactory.php';
include_once './dbconfig.php'; // $config is defined here
include_once './test_setup.php'; // refreshTestData() is defined here

try
{
    // Test positional/question marks parameters
    echo 'Prepared statement with named parameters test: <br />';
    refreshTestData($config);
    // /home/web/windows.php.net/docroot/downloads/snaps/
    $conn   = Pone_Database_ConnectionFactory::getConnection($config);
    $sql    = 'INSERT INTO test2 (id, username) VALUES (?, ?)';
    $pstmt  = $conn->prepareStatement($sql);
    $params = array(
    0 => array('value' => 1000, 'type' => 'int'),
    1 => 'pcdinh1000'
    );
    $pstmt->bindValues($params);
    $rsFound = $pstmt->execute();

    echo '<br />';
    assertTrue($rsFound === false, 'Expected: Pone_Database_MySQLPreparedStatement::execute() should return false when the statement is an INSERT. ');

    $sql    = 'SELECT * FROM test2 WHERE id = ? AND username = ?';
    $pstmt  = $conn->prepareStatement($sql);
    $params = array(
    0 => array('value' => 1000, 'type' => 'int'),
    1 => 'pcdinh1000'
    );
    $pstmt->bindValues($params);
    $rsFound = $pstmt->execute();

    echo '<br />';
    assertTrue($rsFound === true,
    'Expected: Pone_Database_MySQLPreparedStatement::execute() should return false when the statement is an SELECT
    which really returns a result set. ');

    $rs = $pstmt->getResultSet();
    echo '<br />';
    assertTrue($rs->isAvailable() === true,
    'Expected: The ResultSet object returns by Pone_Database_MySQLPreparedStatement::getResultSet() should be in AVAILABLE state. ');

    echo '<br />';
    assertTrue($rs->rowCount() === 1,
    'Expected: The ResultSet object returns by Pone_Database_MySQLPreparedStatement::getResultSet() contains a row. ');

    echo '<br />';
    assertTrue($rs->getAssociativeArray() === array( 0 => array('id' => 1000, 'username' => 'pcdinh1000', 'seq_number' => 1)),
    'Expected: The ResultSet object returns by Pone_Database_MySQLPreparedStatement::getResultSet() contains a row that includes
    the data inserted from the preceding SQL statement. ');

    // Free resources
    $pstmt->close();
}
catch (Pone_Exception_DatabaseAccess $ex)
{
    echo implode('. ', $ex->getTraceMessages());
    echo '<br />';
    echo $ex->getNativeMessage();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

$conn->close();
?>